package com.zhyt.intelligentpipeline.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.zhyt.intelligentpipeline.model.Pipeline;
import com.zhyt.intelligentpipeline.model.Station;
import com.zhyt.intelligentpipeline.service.StationService;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class PipelineWrap {

    String id;


    Station initialStation;


    Station endStation;

    String trunkName;

    int index;


    public static List<PipelineWrap> convert(List<Pipeline> pipelines, StationService stationService){
        List<PipelineWrap> pipelineWraps = new ArrayList<>();
        String start = "";
        for (Pipeline pipeline : pipelines) {
            String startName = pipeline.getInitialStation();
            boolean isStart = true;
            for (Pipeline pipeline1 : pipelines) {
                if (pipeline1.getEndStation().equals(startName)) {
                    isStart = false;
                    break;
                }
            }
            if(isStart){
                start = startName;
                break;
            }
        }
        int index = 0;
        boolean flag;
        while (!"".equals(start)){
            flag = false;
            for (Pipeline pipeline : pipelines) {
                if(start.equals(pipeline.getInitialStation())){
                    flag = true;
                    PipelineWrap temp = new PipelineWrap();
                    temp.setTrunkName(pipeline.getTrunkLine());
                    temp.setId(pipeline.getId());
                    temp.setIndex(index);
                    temp.setInitialStation(stationService.getStationByName(pipeline.getInitialStation()));
                    temp.setEndStation(stationService.getStationByName(pipeline.getEndStation()));
                    index++;
                    start = pipeline.getEndStation();
                    pipelineWraps.add(temp);
                    break;
                }
            }
            if(!flag){
                start = "";
            }
        }

        return pipelineWraps;
    }

    public static PipelineWrap pipelineToPipelineWrap(Pipeline pipeline, StationService stationService) {
        PipelineWrap pipelineWrap = new PipelineWrap();
        pipelineWrap.setId(pipeline.getId());
        pipelineWrap.setInitialStation(stationService.getStationByName(pipeline.getInitialStation()));
        pipelineWrap.setEndStation(stationService.getStationByName(pipeline.getEndStation()));
        pipelineWrap.setIndex(0);
        pipelineWrap.setTrunkName(pipeline.getTrunkLine());
        return pipelineWrap;

    }
}
